package com.cxh.service.impl;

import com.cxh.base.Data1ServiceImpl;
import com.cxh.base.Data2ServiceImpl;
import com.cxh.util.DataSource;
import com.cxh.dao.CmdCollectionDao;
import com.cxh.dao.RobotMapLibDao;
import com.cxh.dao.SkillsOptionCmdDao;
import com.cxh.dao.SkillsOptionDao;
import com.cxh.dao.SysVarsDao;
import com.cxh.entity.SkillsOption;
import com.cxh.service.SkillsOptionService;
import com.cxh.util.CommonUtility;
import com.cxh.util.StringUtils;
import com.cxh.entity.CmdCollection;
import com.cxh.entity.RobotMapLib;
import com.cxh.entity.SkillsOptionCmd;
import com.cxh.util.ComUtil;
import com.shimne.util.NumberUtil;
import com.shimne.util.ObjectUtil;
import com.shimne.util.StringUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类说明：
 * @author  ChenXH
 * @date  2019-01-04 15:22:57
 */
@Service("skillsOptionService")
@DataSource("dataSource1")
public class SkillsOptionServiceImpl extends Data1ServiceImpl<SkillsOption> implements SkillsOptionService {
    @Autowired
    private SkillsOptionDao dao;
    @Autowired
    private SysVarsDao sysVarsDao;
    @Autowired
    private SkillsOptionCmdDao skillsOptionCmdDao;
    @Autowired
    private CmdCollectionDao cmdCollectionDao;
    @Autowired
    private RobotMapLibDao robotMapLibDao;
    

    /*@Override
    public List<SkillsOption> loadPageList(Map<String, Object> map) {
       return dao.loadPageList(map);
    }*/
    
    @Override
    public List<Map<String, Object>> LoadSkills(String answer, Integer robotId) {
    	List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		if(robotId < 0 || CommonUtility.isEmpty(answer)){
			return null;
		}
		List<Map<String,Object>> optionList = dao.newloadOptionsByRobotId(robotId);	//获得用户的定制技能
		list = this.mateString(answer, optionList);
		if(list==null || list.size()<=0){
			optionList=dao.loadOptionsByRobotId(robotId);	//获得机器人所属企业下所有的机器人技能
			list=this.mateString(answer, optionList);
		}
		return list;
    }
    
    
    /**
	 * 根据关键词字符串拼装正则表达式
	 * @param value
	 * @return
	 *
	 * @update 修改 将传入的字符串 转换为数组，再进行排序
	 * @author Y
	 * @date 2018年9月5日
	 */
	private String makeStr(String value){
		value=value.replaceAll(" ","");
		if(StringUtil.isEmpty(value)){
			return null;
		}

			//将中文逗号修改为应为逗号
			value=value.replaceAll("，", ",");
			StringBuffer str=new StringBuffer("(");
			String[] values=value.split(",");
			Arrays.sort(values, new Comparator<String>() {
				@Override
				public int compare(String s1, String s2) {
					int i = s1.length();
					int j = s2.length();
					if (i > j) {return -1;}
					if (i < j) {return 1;}
					return 0;
				}
			});
			for(int i=0;i<values.length;i++){
				if(StringUtil.isTrimEmpty(values[i]) || values[i].length()<2){
					continue;
				}
				str.append(values[i]);
				if(i<values.length-1){
					str.append("|");
				}
			}
			String checkStr="(.)+\\|$";
//			if(StringUtils.checkRegular(checkStr, str.toString())){
//				return null;
//			}
			
			if(com.cxh.util.StringUtils.checkRegular(checkStr, str.toString())){
				str=new StringBuffer(str.toString().substring(0,str.toString().length()-1));
			}
			str.append(")");
			if(str==null || "()".equals(str.toString())){
				return null;
			}
			return str.toString();

	}
	
	private List<Map<String,Object>> mateString(String answer,List<Map<String,Object>> list){
		if(list==null && list.size()<=0){
			return null;
		}
		int maxLength=0;
		List<Map<String,Object>> objList=new ArrayList<Map<String,Object>>();
		for(Map<String,Object> objMap:list){
			if(objMap==null || objMap.size()<=0 || ObjectUtil.isNull(objMap.get("keywords"))){
				continue;
			}
			if(NumberUtil.parseInt(String.valueOf(objMap.get("optionid")))==1221){
				String keyWords=String.valueOf(objMap.get("keywords"));
				System.out.println(this.makeStr(keyWords));
			}
			String str=this.makeStr(String.valueOf(objMap.get("keywords")));
			boolean bean=StringUtil.isTrimEmpty(str)?false:com.cxh.util.StringUtils.checkRegular(str,answer)	;
			if(bean){
				//取匹配最大的数据
				String keyWordString=com.cxh.util.StringUtils.getStringValue(str,answer);
				if(!CommonUtility.isEmpty(keyWordString)){
					if(keyWordString.length()>maxLength){
						objList.clear();
						objList.add(objMap);
						maxLength=keyWordString.length();
					}
					else if(keyWordString.length()==maxLength){
						objList.add(objMap);
					}
				}

			}
		}
		return objList;
	}
	
	@Override
	public SkillsOption findOptionBySkill(int skillId, String answer) {
		//1.首先获得场景下所有的二级问答列表并进行匹配
		List<SkillsOption> list = dao.findOptionBySkill(skillId);
		String Strings = "(带我去|领我去|带我过去|领我过去|带我到|领我到|带客人去|带客人到)";
		
		SkillsOption optionObj=null;
		if(list!=null && list.size()>0){
			for(SkillsOption skillsOption:list){
				if(ObjectUtil.isNull(skillsOption)||StringUtil.isTrimEmpty(skillsOption.getKeywords())){
					continue;
				}
				String[] keywords = skillsOption.getKeywords().split(",");
				StringBuffer str = new StringBuffer("(");
				int length = keywords.length;
				for(int i=0; i<length; i++){
					str.append(keywords[i]);
					if(i<length-1){
						str.append("|");
					}
				}
				str.append(")");
				if(StringUtils.checkRegular(str.toString(), answer) && StringUtils.checkRegular(Strings, answer)){
					return skillsOption;
				}else{
					if(StringUtils.checkRegular(str.toString(), answer)){
						optionObj=skillsOption;
					}
				}
			}
		}
		return optionObj;
	}
	
	@Override
	public SkillsOption loadById(int optionId) {
		System.out.println();
		if(optionId<=0){
			return null;
		}
		return dao.loadById(optionId);
	}
	
	@Override
	public Map<String, Object> loadByCmd(Integer optionId) {
		if(optionId<=0){
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String , Object>> lstMap =  dao.loadByCmd(optionId);
		/*if(lstMap.size() > 0){
			SkillsOptionCmd skillsOptionCmd = skillsOptionCmdDao.findByOptionId(optionId);
			if(ObjectUtil.notNull(skillsOptionCmd)){
				CmdCollection ccl =  cmdCollectionDao.findById(skillsOptionCmd.getCmd());
				map.put("cmdName", ccl.getName());
				int mapid =ObjectUtil.isNull(ccl.getMapid())?0:ccl.getMapid();
				String mapName = "";
				String mapUrl = "";
				if(mapid > 0 && mapid != 0){
					map.put("mapId", mapid);
					RobotMapLib rm = robotMapLibDao.findById(mapid);
					if(!ObjectUtil.isNull(rm)){
						mapName = rm.getFilename();
						map.put("mapName", rm.getMapname());
					}else{
						map.put("mapName", "");
					}
				}
				int size=ccl.getCirculation()>0?ccl.getCirculation():0;
				map.put("circulation", size);	//循环次数
				// 准备访问地址
				String url = sysVarsDao.loadByName(ComUtil.SYSTEM_PATH).getValues()
						+ sysVarsDao.loadByName(ComUtil.SYSINFO_VIRTUALPATH).getValues()
						+ sysVarsDao.loadByName(ComUtil.MAP_PATH).getValues();
				if(!StringUtil.isEmpty(mapName)){
					mapUrl = url + mapName;
				}
				if(!StringUtil.isEmpty(mapUrl)){
					map.put("mapUrl", mapUrl);
				}else{
					map.put("mapUrl", "");
				}
			}
		}*/
		map.put("cmdList", lstMap);
		return map;
	}


	@Override
	public List<SkillsOption> loadPageList(Map<String, Object> map) {
		return null;
	}
}
